package com.disk.config;

import com.disk.filter.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

@Configuration
public class SecurityFilterConfig {

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Bean
    public SecurityFilterChain sysSecurityFilterChain(HttpSecurity http
                                                      ) throws Exception {

        var getIgnoreToken = new String[]{"/user/**", "/doc.html", "/swagger-ui/**",
                "/swagger-ui.html",
                "/swagger-ui/index.html",
                "/swagger-ui.html/**",
                "/webjars/**",
                "/*/api-docs",
                "/v2/**",
                "/**/swagger-resources" ,
                "/**/swagger-resources/configuration/ui" ,
                "/**/swagger-resources/configuration/security"};

        http.requestMatcher(new AntPathRequestMatcher( "/**"))
                .authorizeRequests()
                .antMatchers(getIgnoreToken).permitAll()
                .anyRequest().fullyAuthenticated()
                .and()
                .csrf().disable()
                .cors()
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        return http.build();
    }
}
